<?php

namespace App\Dao\Task;

use App\Dao\User\WechatUserDao;
use App\Service\Pay\AliPay;
use App\Service\Pay\PayFactory;
use Yj\Daos\BaseDao;
use App\Model\Task\TaskPayment;
use Hyperf\Database\Model\Builder;
use Yj\Daos\Verify;
use Hyperf\Utils\Collection;

/**
 * @var TaskPayment
 */
class TaskPaymentDao extends BaseDao
{
    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify("id", function (Builder $query, $id) {
                $query->where("id", $id);
            });
        });
    }

    public function lists(Collection $params)
    {
        $data = $this->DaoWhere($params)->orderByDesc("id")->getList();
        return $data;
    }

    public function add(Collection $params)
    {
        
        $data = $this->create($params->toArray());
        return $data;
    }

    public function edit(int $id, Collection $params)
    {
        $data = $this->DaoWhere(["id" => $id])->firstOrError();
        $data->fill($params->toArray());
        return $data->save();
    }

    public function getWxPayment(mixed $id, $userId)
    {
        $openid = App(WechatUserDao::class)->getOpenid($userId);
        $data = $this->DaoWhere(["id" => $id])->firstOrError();
        $payFactory = PayFactory::wechat();
        $payFactory->setPrice(0.01);
        $payFactory->setTitle($data->id);
        $payFactory->setOrderSn($data->id);
        $payFactory->setOpenid($openid);
        $payFactory->setNotifyUrl('/notify/task/wx');
        $url = $payFactory->exec();
        return $url;
    }

    public function getAliPayPayment(mixed $id)
    {
        $data = $this->DaoWhere(["id" => $id])->firstOrError();
        /** @var AliPay $payFactory */
        $payFactory = PayFactory::create('AliPay');
        $payFactory->setPrice(0.01);
        $payFactory->setTitle($data->id);
        $payFactory->setOrderSn($data->id);
//        $payFactory->setOpenid($openid);
        $payFactory->setNotifyUrl('/notify/task/alipay');
        dump($payFactory);
        $url = $payFactory->exec();
        return $url;
    }
}
